package top.llove.utils;

import cn.hutool.setting.Setting;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;
import top.llove.utils.time.TimeUtils;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * 文件工具类
 *
 * @author: 幸会
 * @date: 2021/6/8 20:21
 * @email: 1846724873@qq.com
 * @description: 文件工具类
 * version: 1.0
 */
@Slf4j
public class FileUtils {

    private static Setting set = new Setting("config.setting");

    private static String p = set.get("xh.img.path") + "imgbed/";

    private static String upload = set.get("xh.img.upload");
    /**
     * 判断指定路径是否存在,不存在则创建
     * @param path
     */
    public static void mkdir(String path) {
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
    }

    public static Map<String, Object> write(MultipartFile file) {
        Map<String, Object> res = new HashMap<>();

        boolean ok = false;

        String type = getType(file); // 获取文件类型

        String path = p + TimeUtils.getTime() + "/" + type; // 文件保存路径根据时间，类型分类

        FileUtils.mkdir(path); // 判断路径是否存在,不存在则创建

        path += "/" + TimeUtils.getTime("yyyyMMddHHmmssSS") + "." + type; // 拼接文件名称


        try(OutputStream os = new FileOutputStream(new File(path))) {
            byte[] imgs = file.getBytes();
            os.write(imgs);
            os.flush();
            log.info("文件保存路径为：" + path);
            res.put("url", path.replace(set.get("xh.img.path"), set.get("xh.img.git.url")));
            ok = true;
        } catch (IOException e) {
            e.printStackTrace();
            ok = false;
        }
        res.put("status", ok);
        return res;
    }

    /**
     * 获取文件类型
     * @param file
     * @return
     */
    public static String getType(MultipartFile file) {
        String filename = file.getOriginalFilename();
        String name = getType(filename);
        return name;
    }

    public static String getType(String filename) {
        int index = filename.lastIndexOf(".") + 1;
        String name = filename.substring(index);
        return name;
    }

    /**
     * 获取带后缀的文件名称获取文件名称
     * @param file
     * @return
     */
    public static String getName(MultipartFile file) {
        String filename = file.getOriginalFilename(); // 获取文件原名称
        String name = getName(filename);
        return name;
    }

    public static String getName(String filename) {
        int index = filename.lastIndexOf(".");
        String name = filename.substring(0, index);
        return name;
    }


    public static Boolean upload(MultipartFile file, String path) throws IOException {

        boolean ok = false;
        try {
            byte[] bytes = file.getBytes(); // 获取文件的字节数组
            upload(bytes, upload + path);
            ok = true;
        } catch (IOException e) {
            e.printStackTrace();
            log.error("写入文件失败, " + e.getMessage());
            ok = false;
        }

        return ok;
    }


    /**
     * 将字节数组写进文件
     * @param bytes 要写进文件的字节数组
     * @param path 文件存放路径
     */
    public static void upload (byte[] bytes, String path) {
        try (OutputStream os = new FileOutputStream(new File(path))) {
            os.write(bytes);
            os.flush();
            log.info("文件写入成功，路径：" + path);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
